package com.loveto.service;

import java.io.UnsupportedEncodingException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.loveto.common.JsonUtils;
import com.loveto.constant.LovetoConstant;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SetupNoticeActivity extends BaseActivity implements
View.OnClickListener {
	
	// 사용하는 변수들
	private Intent intentMoveToPage;
	private ProgressDialog progressDialog;
	private Handler myHandler = new Handler();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loveto_setup_notice);
		
		//변수 초기화
		progressDialog = new ProgressDialog(this);
		
		//클릭 리스너 달기
		((RelativeLayout)findViewById(R.id.btn_goto_back)).setOnClickListener(this);
		
		if (isNetworkAvailableAndNotice())
		getNoticeList();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.area_notice_title:
				int noticeID = Integer.parseInt(((TextView)v.findViewById(R.id.text_noticeid)).getText().toString());
				intentMoveToPage = new Intent(SetupNoticeActivity.this,SetupNoticeBodyActivity.class);
				intentMoveToPage.putExtra("noticeID", noticeID);
				startActivity(intentMoveToPage);
				break;
			case R.id.btn_goto_back:
				finish();
				break;
		}
	}
	
	private void getNoticeList() {
		progressDialog.setMessage("정보를 불러오고 있습니다.");
		progressDialog.show();
		Runnable runnable = new Runnable(){
			@Override
			public void run() {
				//UI 를 건들기 때문에 핸들러 사용.
				myHandler.post(new Runnable() {
					@Override
					public void run() {
							Log.d("tag", "getNoticeList start");
							
							try {
								//서버에 정보 보내기
								String jsonText = JsonUtils.getStringFromUrl(LovetoConstant.SETUP_NOTICE_LIST);
								Log.d("tag", "jsonText : " + jsonText);
								//JSONArray jsonArray = new JSONArray(jsonText);
								JSONArray noticeJSONArray = new JSONArray(jsonText);
								Log.d("tag", "noticeJSONArray:" + noticeJSONArray.toString());
								int jsonArrayLen = noticeJSONArray.length();
								Log.d("tag", "jsonArrayLen = " + jsonArrayLen);
								
								for (int i = 0; i < jsonArrayLen; i++) {
									JSONObject object = noticeJSONArray.getJSONObject(i);
									
									//값 추출
									String noticeTitle = object.getString("notice_title");
									String noticeRegdate = object.getString("regdate");
									int noticeID = object.getInt("noticeid");
									
									// 하나의 row 만들기
									RelativeLayout noticeLayout = (RelativeLayout)getLayoutInflater().inflate(R.layout.loveto_setup_notice_list, null);
									((TextView)noticeLayout.findViewById(R.id.text_title)).setText(noticeTitle + " (" + noticeRegdate + ")");
									((TextView)noticeLayout.findViewById(R.id.text_noticeid)).setText("" + noticeID);
									//클릭 리스너달기
									setClicker((RelativeLayout)noticeLayout.findViewById(R.id.area_notice_title));
									
									LinearLayout v_content_main = (LinearLayout) findViewById(R.id.content_main); 
									v_content_main.addView(noticeLayout);
									
								}
							} catch (JSONException e) {
								e.printStackTrace();
								Log.d("tag", "setRoundInfo() error, " + e.getMessage());
							} catch (UnsupportedEncodingException e) {
								e.printStackTrace();
								Log.d("tag", "setRoundInfo() error, " + e.getMessage());
							} finally {
							}
							progressDialog.dismiss();
							Log.d("tag", "getNoticeList end");
							
						} //Handler run End
				});
			}
		};
		new Thread(runnable).start();
	}
	
	private void setClicker(RelativeLayout layout) {
		layout.setOnClickListener(this);
	}

}
